<template>
  <div class="chat-container">
    <div 
      class="chat-toggle" 
      @click="toggleChat"
      :class="{ 'open': isChatOpen }"
    >
      <span v-if="!isChatOpen" v-text="t('chat.title')"></span>
      <span v-else v-text="t('chat.collapse')"></span>
    </div>
    <div 
      class="chat-box" 
      v-if="isChatOpen"
    >
      <div class="chat-header">
        <h3 v-text="t('chat.title')"></h3>
        <button @click="toggleChat" v-text="t('chat.collapse')"></button>
      </div>
      <div class="chat-messages">
        <div 
          v-for="(message, index) in messages" 
          :key="index"
          :class="['message', message.sender]"
        >
          <div class="message-content" v-text="message.text">
          </div>
          <div class="message-time" v-text="message.timestamp.toLocaleTimeString()">
          </div>
        </div>
      </div>
      <div class="chat-input">
        <input 
          v-model="newMessage" 
          @keyup.enter="sendMessage" 
          :placeholder="t('chat.placeholder')"
        >
        <button @click="sendMessage" v-text="t('chat.send')"></button>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { injectLanguage } from '../utils/language-store.js'

export default {
  name: 'ChatWidget',
  setup() {
    const { t } = injectLanguage()
    
    // 在线客服相关状态
    const isChatOpen = ref(false)
    const messages = ref([])
    const newMessage = ref('')
    
    // 切换聊天窗口显示状态
    const toggleChat = () => {
      isChatOpen.value = !isChatOpen.value
    }
    
    // 发送消息
    const sendMessage = () => {
      if (newMessage.value.trim()) {
        messages.value.push({
          text: newMessage.value,
          sender: 'user',
          timestamp: new Date()
        })
        
        // 模拟客服回复
        setTimeout(() => {
          messages.value.push({
            text: t('chat.agentReply'),
            sender: 'agent',
            timestamp: new Date()
          })
        }, 1000)
        
        newMessage.value = ''
      }
    }
    
    return {
      isChatOpen,
      messages,
      newMessage,
      toggleChat,
      sendMessage,
      t
    }
  }
}
</script>

<style scoped>
/* 在线客服样式 */
.chat-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 1000;
}

.chat-toggle {
  background: #667eea;
  color: white;
  border: none;
  padding: 12px 20px;
  border-radius: 25px;
  cursor: pointer;
  box-shadow: 0 2px 10px rgba(0,0,0,0.2);
  transition: all 0.3s ease;
}

.chat-toggle:hover {
  background: #5a6fd8;
}

.chat-toggle.open {
  border-radius: 25px 25px 0 0;
}

.chat-box {
  width: 350px;
  height: 400px;
  background: white;
  border-radius: 10px 10px 0 10px;
  box-shadow: 0 5px 15px rgba(0,0,0,0.2);
  display: flex;
  flex-direction: column;
  margin-top: 5px;
}

.chat-header {
  background: #667eea;
  color: white;
  padding: 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-radius: 10px 10px 0 0;
}

.chat-header button {
  background: none;
  border: none;
  color: white;
  font-size: 24px;
  cursor: pointer;
}

.chat-messages {
  flex: 1;
  padding: 15px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.message {
  max-width: 70%;
  padding: 10px 15px;
  border-radius: 18px;
  font-size: 14px;
}

.message.user {
  align-self: flex-end;
  background: #667eea;
  color: white;
  border-bottom-right-radius: 5px;
}

.message.agent {
  align-self: flex-start;
  background: #f1f1f1;
  color: #333;
  border-bottom-left-radius: 5px;
}

.message-content {
  margin-bottom: 5px;
}

.message-time {
  font-size: 11px;
  opacity: 0.7;
  text-align: right;
}

.chat-input {
  display: flex;
  padding: 15px;
  border-top: 1px solid #eee;
}

.chat-input input {
  flex: 1;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 20px;
  outline: none;
}

.chat-input button {
  margin-left: 10px;
  padding: 10px 15px;
  background: #667eea;
  color: white;
  border: none;
  border-radius: 20px;
  cursor: pointer;
}

@media (max-width: 768px) {
  .chat-box {
    width: 300px;
    height: 350px;
  }
}
</style>